import 'package:flutter_test/flutter_test.dart';
import 'package:qq/News.dart';

void main() {
  group('News Class Tests', () {
    test('测试通过构造函数初始化 News 对象时，各字段是否正确赋值', () {
      final news = News(
        code: 200,
        msg: 'Success',
        ctime: '2024-11-27',
        title: 'Test Title',
        description: 'Test Description',
        picUrl: 'http://example.com/image.jpg',
        url: 'http://example.com',
      );

      expect(news.code, 200);
      expect(news.msg, 'Success');
      expect(news.ctime, '2024-11-27');
      expect(news.title, 'Test Title');
      expect(news.description, 'Test Description');
      expect(news.picUrl, 'http://example.com/image.jpg');
      expect(news.url, 'http://example.com');
    });

    test('测试 fromJson 工厂方法是否能够正确地从 JSON 创建一个 News 对象', () {
      final json = {
        'code': 200,
        'msg': 'Success',
        'ctime': '2024-11-27',
        'title': 'Test Title',
        'description': 'Test Description',
        'picUrl': 'http://example.com/image.jpg',
        'url': 'http://example.com',
      };

      final news = News.fromJson(json);

      expect(news.code, json['code']);
      expect(news.msg, json['msg']);
      expect(news.ctime, json['ctime']);
      expect(news.title, json['title']);
      expect(news.description, json['description']);
      expect(news.picUrl, json['picUrl']);
      expect(news.url, json['url']);
    });

    test('测试 toMap 方法是否能够正确地将 News 对象转换为字典', () {
      final news = News(
        code: 200,
        msg: 'Success',
        ctime: '2024-11-27',
        title: 'Test Title',
        description: 'Test Description',
        picUrl: 'http://example.com/image.jpg',
        url: 'http://example.com',
      );

      final map = news.toMap();

      expect(map['code'], news.code);
      expect(map['msg'], news.msg);
      expect(map['ctime'], news.ctime);
      expect(map['title'], news.title);
      expect(map['description'], news.description);
      expect(map['picUrl'], news.picUrl);
      expect(map['url'], news.url);
    });
  });
}